package com.api.apigateway.mq;


import com.api.common.constant.APIInterfaceMqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 接口调用次数补偿消息生产者
 */
@Component
@Slf4j
public class CompensateMessageProducer {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送补偿消息到队列
     *
     * @param message JSON格式的补偿消息
     */
    public void send(String message){
        try {
            rabbitTemplate.convertAndSend(
                    APIInterfaceMqConstant.API_INVOKE_COUNT_COMPENSATION_EXCHANGE,
                    APIInterfaceMqConstant.API_INVOKE_COUNT_COMPENSATION_ROUTE_KEY,
                    message
            );
            log.info("发送调用次数补偿消息成功: {}", message);
        } catch (Exception e) {
            log.error("发送调用次数补偿消息失败: message={}", message, e);
            // 可以考虑将失败消息写入数据库或本地文件作为兜底
        }
    }
}
